package com.cn.admin.controller;

import com.cn.admin.controller.api.ProductAPI;
import com.cn.admin.vo.product.AddProductVO;
import com.cn.admin.vo.product.DelectProductVO;
import com.cn.admin.vo.product.FindAllProductVO;
import com.cn.api.ProductCoreService;
import com.cn.api.TestCoreService;
import com.cn.common.parameter.Paging;
import com.cn.common.result.Result;
import com.cn.dto.ProductDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: ProductController
 * @Author: ZhangMao
 * @Date: 2022-02-16
 * 产品信息的crud
 * 后台管理系统
 */
@Slf4j
@Api(tags = "后台管理产品")
@RestController
@RequestMapping("/back/product")
public class ProductController implements ProductAPI {
    @Resource
    private ProductCoreService productCoreService;
    @Resource
    private TestCoreService testCoreService;



    @ApiOperation(value = "添加产品")
    @PostMapping("/add")
    @ResponseBody
    @Override
    public Result add(AddProductVO addProductVO) {
        return null;
    }

    @ApiOperation(value = "删除产品")
    @GetMapping(value = "/delete")
    @ResponseBody
    @Override
    public Result delete(DelectProductVO delectProductVO) {
        return null;
    }

    @ApiOperation(value = "查询所有产品")
    @GetMapping(value = "/findAll")
    @ResponseBody
    @Override
    public Result<List<FindAllProductVO>> findAll(Paging pagingVO) {
        return null;
    }

    @ApiOperation(value = "根据id查询产品")
    @GetMapping(value = "/findById")
    @ResponseBody
    @Override
    public Result<FindAllProductVO> findById(DelectProductVO delectProductVO) {
        return null;
    }

    @ApiOperation(value = "修改产品信息")
    @PostMapping("/update")
    @ResponseBody
    @Override
    public Result update(AddProductVO addProductVO) {
        return null;
    }


    /**
     * redis分布式锁测试
     *
     * @param addProductVO
     * @return
     */
    @ApiOperation(value = "redis分布所")
    @PostMapping("/modification")
    @ResponseBody
    @Override
    public Result modification(AddProductVO addProductVO) {
        Jedis jedis = new Jedis("127.0.0.1",6379);

        ProductDTO productDTO = new ProductDTO();
        productDTO.setId(addProductVO.getId());
        productDTO.setType(addProductVO.getType());
//        System.out.println(productDTO);
//        log.info("\n产品应用层：", productDTO);
//        int result = testCoreService.modification(productDTO);
//        if (result == 1){
//            return Result.success(null,"锁操作成功");
//        }else {
//            return Result.failed("锁操作失败");
//        }

        if (jedis.setnx("version", "aaa") == 1L) {
            jedis.expire("version", 10);
            try {
                //具体业务
                int re = testCoreService.modification(productDTO);
                if (re == 1){
                    System.out.println("操作成功");

                }else {
                    System.out.println("操作失败");
                }

            } finally {
                jedis.del("version");
            }
        }
        else {
            System.out.println("强锁失败");
        }
        return  null;

    }

}
